﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using AFT.Parking.Bizlogic.Parkings;
using AFT.Parking.Entity;
using AFT.Parking.Entity.ArgumentEntity;
using AFT.Parking.Foundation.Extensions;
using AFT.Parking.WebSite.Common;
using AFT.Parking.WebSite.Filter;
using Newtonsoft.Json;

namespace AFT.Parking.WebSite.Controllers
{
    [AdminOnly]
    public class ParkingCameraController : Controller
    {


        private readonly ParkingBiz _parkingBiz;
        public ParkingCameraController()
        {
            _parkingBiz = new ParkingBiz();
        }
        #region parking camera equipment
        //
        // GET: /ParkingCamera/
        /// <summary>
        /// 
        public ActionResult CameraList(string name, int parkingAreaId = 0, int page=1)

        {
            Expression<Func<ParkingMonitor, bool>> exp = u => true;
            if (!name.IsNullOrEmpty())
            {
                exp = u => u.Name.Contains(name);
            }
            if (parkingAreaId > 0)
            {
                exp = u => u.ParkingAreaId == parkingAreaId;
                ViewBag.ParkingAreaId = parkingAreaId;
                var area = _parkingBiz.GetParkingArea(parkingAreaId);
                ViewBag.ParkingAreaName = area.Name;

                var areaList = _parkingBiz.GetAllAreaShares().OrderBy(a => a.Id).Select(a => a.ShareName).ToArray();
                ViewBag.AreaIdList = areaList;
            }
            var p = new PaginationParam<ParkingMonitor>
            {
                Expression = exp,
                PageSize = 20,
                PageIndex = page,
                OrderByList = new List<OrderBy<ParkingMonitor>>
                {
                    new OrderBy<ParkingMonitor>() {Sort = false, exp = u => u.Id}
                }
            };
            return View(_parkingBiz.MonitorList(p));
        }

        public ActionResult AddCamera(int parkingAreaId)
        {
            var area = _parkingBiz.GetParkingArea(parkingAreaId);
            var areaShares = _parkingBiz.GetAreaShares(parkingAreaId);
            ViewBag.AreaShares = areaShares;
            ViewBag.ParkingAreaName = area.Name;
            return View(new ParkingMonitor { ParkingAreaId = parkingAreaId });
        }

        [HttpPost]
        public ActionResult AddCamera(ParkingMonitor monitor)
        {
            string msg = "";
            var icon = "error";
            if (monitor.Name.IsNullOrEmpty())
                msg = "设备名称不能为空；";
            if (monitor.ShareFrom <= 0 || monitor.ShareTo <= 0)
                msg += "设备监测的车牌区域不能为空；";
            if (_parkingBiz.ValidateIp(monitor))
            {
                msg += "存在相同Ip地址的设备";
            }
            if (msg.Length == 0)
            {
                if (_parkingBiz.SaveCamera(monitor))
                {
                    icon = "success";
                    msg = "保存成功。";
                }
                else
                    msg = "保存失败，存在相同的店铺信息。";
            }

            ViewBag.Message = msg;
            ViewBag.Icon = icon;
            if (icon == "error")
            {
                return View(monitor);
            }
            return Redirect("/ParkingCamera/CameraList?parkingAreaId=" + monitor.ParkingAreaId);
        }
        [HttpGet]
        public ActionResult EditCamera(int id)
        {
            var camera = _parkingBiz.GetCamera(id);
            var area = _parkingBiz.GetParkingArea(camera.ParkingAreaId);
            var areaShares = _parkingBiz.GetAreaShares(camera.ParkingAreaId);
            ViewBag.AreaShares = areaShares;
            ViewBag.ParkingAreaName = area.Name;
            return View(camera);
        }

        [HttpPost]
        public ActionResult EditCamera(ParkingMonitor monitor)
        {
            string msg = "";
            var icon = "error";
            if (monitor.Name.IsNullOrEmpty())
                msg = "设备名称不能为空；";
            if (monitor.ShareFrom <= 0 || monitor.ShareTo <= 0)
                msg += "设备监测的车牌区域不能为空；";
            if (_parkingBiz.ValidateIp(monitor))
            {
                msg += "存在相同Ip地址的设备";
            }
            if (msg.Length == 0)
            {
                if (_parkingBiz.SaveCamera(monitor))
                {
                    icon = "success";
                    msg = "保存成功。";
                }
                else
                    msg = "保存失败，存在相同的店铺信息。";
            }

            ViewBag.Message = msg;
            ViewBag.Icon = icon;
            if (icon == "error")
            {
                return View(monitor);
            }
            return Redirect("/ParkingCamera/CameraList?parkingAreaId=" + monitor.ParkingAreaId);
        }

        #endregion

        #region parking areaShares

        /// </summary>
        /// <param name="id">id:ParkingAreaId</param>
        /// <returns></returns>
        public ActionResult Add(int parkingAreaId)
        {
            ViewBag.ParkingAreaName = _parkingBiz.GetParkingArea(parkingAreaId).Name;
            return View(new ParkingAreaShare { ParkingAreaId = parkingAreaId });
        }

        [HttpPost]
        public ActionResult Add(ParkingAreaShare parkingAreaShare)
        {
            var msg = "";
            var icon = "error";
            if (parkingAreaShare.ShareName.IsNullOrEmpty())
            {
                msg += "停车区名称不能为空";
            }
            if (parkingAreaShare.HighlightImg.IsNullOrEmpty())
            {
                msg += "停车区图片不能为空";
            }
            if (msg.Length == 0)
            {
                parkingAreaShare.HighlightImg = FileHelper.Save("AreaShares", parkingAreaShare.HighlightImg);
                if (_parkingBiz.SaveAreaShare(parkingAreaShare))
                {
                    icon = "success";
                    msg = "保存成功。";
                }
                else
                    msg = "保存失败，存在相同的店铺信息。";
            }
            ViewBag.Message = msg;
            ViewBag.Icon = icon;
            return View(parkingAreaShare);

        }

        public ActionResult EditAreaShare(int id)
        {
            var areaShare = _parkingBiz.GetAreaShare(id);
            ViewBag.ParkingAreaName = _parkingBiz.GetParkingArea(areaShare.ParkingAreaId).Name;
            return View(areaShare);
        }

        [HttpPost]
        public ActionResult EditAreaShare(ParkingAreaShare parkingAreaShare)
        {
            var msg = "";
            var icon = "error";
            if (parkingAreaShare.ShareName.IsNullOrEmpty())
            {
                msg += "停车区名称不能为空";
            }
            if (parkingAreaShare.HighlightImg.IsNullOrEmpty())
            {
                msg += "停车区图片不能为空";
            }
            if (msg.Length == 0)
            {
                parkingAreaShare.HighlightImg = FileHelper.Save("AreaShares", parkingAreaShare.HighlightImg);
                if (_parkingBiz.SaveAreaShare(parkingAreaShare))
                {
                    icon = "success";
                    msg = "保存成功。";
                }
                else
                    msg = "保存失败，存在相同的店铺信息。";
            }
            ViewBag.Message = msg;
            ViewBag.Icon = icon;
            if (icon == "error")
            {
                return View(parkingAreaShare);
            }
            return Redirect("/ParkingCamera/AreaShares?parkingAreaId=" + parkingAreaShare.ParkingAreaId);
        }
        [HttpPost]
        public ActionResult DelAreaShare(int id)
        {
            var areaShare = _parkingBiz.GetAreaShare(id);
            _parkingBiz.DelAreaShare(areaShare);
            return Content("1");
        }
        public ActionResult FileUpLoadFile(string folderName)
        {
            var result = FileHelper.Upload(folderName);
            return Content(JsonConvert.SerializeObject(result));
        }

        public ActionResult AreaShares(string name, int parkingAreaId = 0, int page = 1)
        {
            Expression<Func<ParkingAreaShare, bool>> exp = u => true;
            if (!name.IsNullOrEmpty())
            {
                exp = u => u.ShareName.Contains(name);
            }
            if (parkingAreaId > 0)
            {
                exp = u => u.ParkingAreaId == parkingAreaId;
                ViewBag.ParkingAreaId = parkingAreaId;
                ViewBag.ParkingAreaName = _parkingBiz.GetParkingArea(parkingAreaId).Name;
            }
            var param = new PaginationParam<ParkingAreaShare>
            {
                PageIndex = page,
                PageSize = 20,
                Expression = exp,
                OrderByList = new List<OrderBy<ParkingAreaShare>> { new OrderBy<ParkingAreaShare> { Sort = false, exp = u => u.Id } }
            };
            
            return View(_parkingBiz.AreaShares(param));
        }
        public ActionResult CarsOfAreas(string number, int parkingAreaShareId = 0, int page = 1,int parkingsId=0)
        {
            Expression<Func<ParkingAreaShareRecord, bool>> exp = u => true;

            if (!number.IsNullOrEmpty())
            {
                exp = u => u.CarNumber.Contains(number);
            }

            if (parkingsId > 0)
            {
                exp = u => u.ParkingId == parkingsId;
                var parking = _parkingBiz.GetSingle(parkingsId);
                if (parking != null) ViewBag.ParkingAreaShareName=parking.Name;
            }

            if (parkingAreaShareId > 0)
            {
                exp = u => u.ParkingAreaShareId == parkingAreaShareId;
                ViewBag.ParkingAreaShareId = parkingAreaShareId;

                var area = _parkingBiz.GetAreaShare(parkingAreaShareId);
                if (area != null) ViewBag.ParkingAreaShareName = area.ShareName;
            }
            
            var param = new PaginationParam<ParkingAreaShareRecord>
            {
                PageIndex = page,
                PageSize = 20,
                Expression = exp,
                OrderByList = new List<OrderBy<ParkingAreaShareRecord>> { new OrderBy<ParkingAreaShareRecord> { Sort = false, exp = u => u.Id } }
            };
            return View(_parkingBiz.CarsOfAreas(param));
        }

        #endregion

        public ActionResult CarShareRecords(string name, int parkingAreaId = 0, int page = 1)
        {
            Expression<Func<CarShareRecord, bool>> exp = u => true;
            //if (parkingAreaId > 0)
            //{
            //    exp = u => u.ParkingAreaId == parkingAreaId;
            //    var areaList = _parkingBiz.GetAllAreaShares().OrderBy(a => a.Id).Select(a => a.ShareName).ToArray();
            //    ViewBag.AreaIdList = areaList;
            //}
            if (!name.IsNullOrEmpty())
                exp = exp.And(u => u.CarNumber.ToLower().Contains(name.ToLower()));
            var p = new PaginationParam<CarShareRecord>
            {
                Expression = exp,
                PageSize = 20,
                PageIndex = page,
                OrderByList = new List<OrderBy<CarShareRecord>>
                {
                    new OrderBy<CarShareRecord> {Sort = false, exp = u => u.Id}
                }
            };
            return View(_parkingBiz.CarShareRecords(p));
        } 
    }
}